GBML manual / Drawing

Screen anchors

An anchor names a spot on the screen, like the top-left corner, so a score or a health bar can say "4 pixels in from there" instead of hiding the size of the screen inside a number.

The trouble with a pixel number

Say you want the score in the top-left corner. Everything drawn in a Draw GUI event, such as with draw_text, counts from the top-left pixel of the screen, so that corner is easy: (0, 0). To put the score 4 pixels in from the edges you write:

// Draw GUI event
draw_text(4, 4, "SCORE");

That works, and it is a good way to place anything near the top-left. The trouble starts at the other corners. The GBA screen is 240 pixels wide, so 4 pixels in from the right edge is 236, and 4 pixels up from the bottom is 156:

// Draw GUI event
draw_text(236, 156, "A: OK");

Look at what those numbers actually say: nothing about a corner, and nothing about "4 pixels in". 236 only means "236 pixels across", and it is only the right-hand corner because the screen happens to be exactly 240 wide. Move the game to a screen of a different size and the number is still 236, so the text is no longer in the corner. Nothing can notice that, because all the game ever saw was a pixel, and never what you meant by it.

An anchor says what you mean

An anchor is a name for one of nine spots on the screen. You say which spot, and then how far to move from it:

// Draw GUI event
draw_text_anchored(ANCHOR_TOP_LEFT, 4, 4, "SCORE");

Read it aloud and it is the sentence you had in mind: start at the top-left corner, then 4 pixels right and 4 down. The number 4 now means only "a small gap", so it is right on any screen. The corner itself is looked up when the game runs, so it is always the real corner.

The nine spots are laid out like the screen:

leftmiddleright
topANCHOR_TOP_LEFTANCHOR_TOP_CENTERANCHOR_TOP_RIGHT
middleANCHOR_MIDDLE_LEFTANCHOR_CENTERANCHOR_MIDDLE_RIGHT
bottomANCHOR_BOTTOM_LEFTANCHOR_BOTTOM_CENTERANCHOR_BOTTOM_RIGHT

How the two numbers work

After the anchor come the same x and y as always, and they are simply added to the anchor's own spot. x moves right and y moves down, like everywhere else in Gbabel. So:

Two things do not change. text_align still decides which part of the text sits on the point, so a line that should end at the right corner needs text_align(ALIGN_RIGHT). And a rectangle's position is its top-left corner, so a bar that starts 4 pixels in from the left edge just says x = 4, and a bar w wide that ends 4 pixels short of the right edge says x = -4 - w from ANCHOR_TOP_RIGHT.

Which calls have an anchored form

Every drawing call that can be fixed to the screen has one. It takes the anchor first, and everything else is the same as the plain call:

Plain callAnchored call
draw_textdraw_text_anchored
draw_text_warpeddraw_text_warped_anchored
draw_text_3ddraw_text_3d_anchored
draw_rectdraw_rect_anchored
draw_rect_filldraw_rect_fill_anchored

An anchored call is fixed to the screen wherever you write it - in a Step event, in a script, in any event - not only in a Draw GUI event. The plain calls only reach the screen inside a Draw GUI event; in a Draw event they put things at a place in the room, and a place in the room is not a place on the screen. A call that draws a sprite (draw_sprite) has no anchored form: draw it in a Draw GUI event.

When you have to do the sum yourself

Some positions are not "a spot plus a nudge". Centring a picture whose size you know, or spacing four icons evenly across the screen, needs the screen's size in a sum. For those, ask the screen instead of typing its size: screen_width and screen_height give back how many pixels wide and tall it is. screen_width() - 4 is right on any screen, and a typed 236 is right on one.

So there are three ways to place something on the screen, and you can mix them freely:

The plain calls in a Draw GUI event

draw_text(4, 4, "SCORE") in a Draw GUI event draws on exactly the same pixel as the anchored version above. It is a good choice for something placed by hand, like a menu whose rows are counted out one by one. Reach for an anchor when the words you would use to describe the spot are "the corner" or "the middle of the bottom": that is the kind of position that should survive a change of screen.

On the GBA

The screen is 240 pixels wide and 160 tall, so on the GBA the nine anchors are these points, counted from the top-left pixel (the way a plain draw call in a Draw GUI event counts): the left column is x = 0, the middle column x = 120 and the right column x = 240; the top row is y = 0, the middle row y = 80 and the bottom row y = 160. An anchored call adds its anchor to (x, y) and then makes the plain call, so it costs exactly the same, and a game that never uses one has nothing added to its cartridge.

See also: Drawing and the screenscreen_widthscreen_heightdraw_text_anchoreddraw_text_warped_anchoreddraw_text_3d_anchoreddraw_rect_anchoreddraw_rect_fill_anchoreddraw_textDrawing text